package com.hzsoft.resolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.hzsoft.sys.entity.SysUser;
import com.hzsoft.util.Const;


public class LoginHandlerInterceptor extends HandlerInterceptorAdapter{

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		
		String path = request.getServletPath();
		if(path.matches(Const.NO_INTERCEPTOR_PATH)){
			return true;
		}else{
			HttpSession session = request.getSession();
			SysUser user = (SysUser)session.getAttribute(Const.SESSION_USER);
			if(user==null) {	
				response.sendRedirect(request.getContextPath()+"/login.do");
				return false;
			}else {	
				return true;
			}
		}
	}
	 @Override  
	   public void postHandle(HttpServletRequest request,HttpServletResponse response, Object handler,  
	            ModelAndView modelAndView) throws Exception { 
		super.postHandle(request, response, handler, modelAndView);  
	 }

	 @Override 
	    public void afterCompletion(HttpServletRequest request, 
	            HttpServletResponse response, Object handler, Exception ex) 
	            throws Exception { 
		 super.afterCompletion(request, response, handler, ex);      
	 }

	/** Default constructor */
	public LoginHandlerInterceptor() {
	}
	
}
